<?php


namespace App\Models\CacheModel\live;


use app\common\lib\Util;
use App\Models\Api\BaseCache;
use think\Cache;
use think\Log;
use AstroTool\Cache\CacheManage;

class LiveSignHotDay extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_sign_hot_day';

    protected $cacheModel = 'live.sign_hot_day';

    /**
     * 每日星座点赞总数
     * @param int $liveId 直播间ID
     * @param $liveRecordId
     * @param $sign // 星座ID
     * @param int $inc
     * @throws \app\common\lib\exception\ApiException
     */
    public function setDaySignInc(int $liveId, $liveRecordId, $sign, $inc = 1)
    {
        $day = (int)(date('Ymd'));
        $cacheKey = 'live:sign_hot_day:info:' . $liveId . ':' . $day;

        try
        {
            $redis = CacheManage::getInstance()->redisHandler;
            if (!($redis->hExists($cacheKey, $sign)))
            {
                try
                {
                    $hot = $this->where(['live_record_id' => $liveRecordId, 'day' => $day, 'sign' => $sign])->value('hot');

                    if (is_null($hot))
                    {
                        $insertData = [
                            'sign' => $sign,
                            'hot' => $inc,
                            'live_id' => $liveId,
                            'live_record_id' => $liveRecordId,
                            'day' => $day,
                            'create_time' => time()
                        ];

                        $this->insert($insertData);
                    } else
                    {
                        $inc = $hot + $inc;
                    }
                }
                catch (\Exception $e)
                {
                }
            }

            $redis->hIncrBy($cacheKey, $sign, $inc);

            $redis->expire($cacheKey, 86400);
        }
        catch (\Exception $e)
        {
            Log::write("点赞缓存更新异常3->1111：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
        }

        // CacheManage::getInstance()->setCacheConfig($this->cacheModel, $cacheKeyId)->setCacheType('hash')->hashDataInc((string)$sign, (int)$inc, function () use ($liveId,$liveRecordId, $day, $sign, $inc)
        // {
        //     try
        //     {
        //         $hot = $this->where(['live_record_id' => $liveRecordId, 'day' => $day, 'sign' => $sign])->value('hot');
        //
        //         if (is_null($hot))
        //         {
        //             $insertData = [
        //                 'sign' => $sign,
        //                 'hot' => $inc,
        //                 'live_id' => $liveId,
        //                 'live_record_id' => $liveRecordId,
        //                 'day' => $day,
        //                 'create_time' => time()
        //             ];
        //
        //             $this->insert($insertData);
        //
        //             return $inc;
        //         } else
        //         {
        //             return $hot + $inc;
        //         }
        //     }
        //     catch (\Exception $e)
        //     {
        //         return 0;
        //     }
        // });

        $isUpdateToData = Cache::get('live_cache_update_to_date:sign_hot_day:' . $liveId, 0);

        if (!$isUpdateToData)
        {
            // 发送队列-》更新缓存内数据到数据库中
            Util::queuePush('live_cache_update_to_database', [
                'type' => 'sign_hot_day',
                'data' => [
                    'day' => $day,
                    'live_id' => $liveId,
                    'live_record_id' => $liveRecordId,
                ],
            ], 60);
            // 记录已添加同步缓存到数据库
            Cache::set('live_cache_update_to_date:sign_hot_day:' . $liveId, 1, 86400);
        }
    }

    /**
     * 获取星座日榜信息
     * @param int $liveId
     * @param int $liveRecordId 直播记录ID
     * @param string $date date('Ymd')
     * @return string
     * @throws \Exception
     */
    public function getDaySignList(int $liveId, int $liveRecordId, string $date)
    {
        $cacheKeyId = $liveId . ':' . $date;

        $resData = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $cacheKeyId)->getDataAndSet(function () use ($liveId, $liveRecordId, $date)
        {
            try
            {
                return $this->where(['live_id' => $liveId, 'live_record_id' => $liveRecordId, 'day' => $date])->column('hot', 'sign');
            }
            catch (\Exception $e)
            {
                return [];
            }
        });

        return $resData;
    }
}
